<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use EasyWeChat\Message\Text;
use EasyWeChat\Message\News;

use App\Models\Shop\Product, App\Models\Shop\Order, App\Models\Shop\Customer;
use EasyWeChat;

class ApiController extends Controller
{
    public function serve()
    {

        $server = EasyWeChat::server();

        $server->setMessageHandler(function ($message) {

            //文本消息
            if ($message->MsgType == 'text') {
                switch ($message->Content) {
                    case 'hot':
                    case '热卖':
                        return $this->is_hot();
                        break;
                }
            }
        });

        return $server->serve();

    }
    //人气热卖
    function is_hot()
    {
        $products = Product::where('is_hot', true)
            ->orderBy('is_top', "desc")
            ->orderBy('created_at')
            ->take(3)
            ->get();
   //return $products;

        $news = [];
        foreach ($products as $p) {
            $news[] = new News([
                'title' => $p->name,
                'description' => $p->desc,
                'url' => 'http://shop.lidongshan.com/product/' . $p->id,
                'image' => 'http://shop.lidongshan.com/' . $p->thumb,
            ]);
        }
        return $news;
    }
}

